
from odoo import api, fields, models, _


class StockLine(models.Model):
    _inherit = 'stock.move.line'

    def get_nd_types(self):
        return [('sale', '销售'),
                ('other_sale', '其他出库'),
                ('sale_return', '销售退货'),
                ('purchase', '采购'),
                ('other_purchase', '其他入库'),
                ('purchase_return', '采购退货'),
                ('internal', '内部移库')]
        # return self.env['pre.account.line']._fields['type'].selection

    def get_sale_type_selection(self):
        return self.env['sale.order']._fields['nd_sale_type'].selection
    def get_transport_type_selection(self):
        return self.env['sale.order']._fields['nd_transport_type'].selection

    nd_type = fields.Selection(selection=get_nd_types, string="作业类型",
                               compute="_compute_nd_type", store=True)
    wms_no = fields.Char(related="picking_id.wms_no", store=True)
    nd_date_done = fields.Datetime('完成时间', related="picking_id.date_done", store=True)
    nd_partner_id = fields.Many2one(related="picking_id.partner_id", store=True, string="合作伙伴")

    nd_categ_id = fields.Many2one('product.category', related="product_id.categ_id",
                                  store=True, string="分组")
    gsp_categ_id = fields.Many2one('gsp.product.categ', related="product_id.gsp_categ_id",
                                   store=True, string="分类")
    nd_poduct_name = fields.Char("产品名称", related="product_id.name", store=True)
    nd_default_code = fields.Char("产品编码", related="product_id.default_code", store=True)
    nd_size = fields.Char(related="product_id.nd_size", store=True, string="规格型号")
    nd_price_unit = fields.Float('含税单价', compute="_compute_nd_price_unit", store=True)
    nd_price_total = fields.Float('含税总价', compute="_compute_nd_price_unit", store=True)
    tax_ids = fields.Many2many('account.tax', compute="_compute_nd_price_unit", store=True, string="税率")
    gift = fields.Boolean('赠品', compute="_compute_nd_price_unit", store=True)
    nd_producer_id = fields.Many2one(string="生产厂家", store=True, related="product_id.producer_id")
    expire_date = fields.Date('有效期至', related="product_id.expire_date", store=True)
    nd_show_production_date = fields.Datetime('生产日期', related="lot_id.nd_production_date", store=True)
    nd_show_expire_date = fields.Datetime('过期日期', related="lot_id.life_date", store=True)

    nd_department_id = fields.Many2one('hr.department', string="所属部门",
                                       compute="_compute_nd_price_unit", store=True)
    nd_sale_type = fields.Selection(selection=get_sale_type_selection, string="交易方式",
                                    compute="_compute_nd_price_unit", store=True)
    nd_transport_type = fields.Selection(selection=get_transport_type_selection, string="运输方式",
                                         compute="_compute_nd_price_unit", store=True)
    nd_hospital_id = fields.Many2one('nd.hospital', string="所属医院",
                                     compute="_compute_nd_price_unit", store=True)
    nd_hospital_department = fields.Char('科室', compute="_compute_nd_price_unit", store=True)
    nd_note = fields.Text('备注', compute="_compute_nd_price_unit", store=True)

    pre_account_line_ids = fields.Many2many('pre.account.line', compute='_compute_pre_account_line_ids', store=True, string='发票明细')
    nd_account_line_count = fields.Integer('发票明细数量', compute="_compute_pre_account_line_ids", store=True)

    @api.depends('pre_account_line_ids')
    def _compute_pre_account_line_ids(self):
        for line in self:
            line.pre_account_line_ids = [(6, 0, line.move_id.pre_account_line_ids.ids)]
            line.nd_account_line_count = len(line.move_id.pre_account_line_ids.ids)

    def action_open_pre_account_line(self):
        self.ensure_one()
        lines_ids = self.pre_account_line_ids
        tree_view = self.env.ref('nd_account.NDPreAccountLineViewTree')
        form_view = self.env.ref('nd_account.NDPreAccountLineViewFORM')
        return {
            'name': '发票明细',
            'type': 'ir.actions.act_window',
            'res_model': 'pre.account.line',
            'views': [(tree_view.id, 'tree'), (form_view.id, 'form')],
            'domain': [('id', 'in', lines_ids.ids)]
        }

    @api.depends('move_id',
                 'move_id.purchase_line_id',
                 'move_id.purchase_line_id.price_unit',
                 'move_id.purchase_line_id.product_uom',
                 'move_id.purchase_line_id.gift',
                 'move_id.purchase_line_id.taxes_id',
                 'move_id.purchase_line_id.order_id',
                 'move_id.sale_line_id',
                 'move_id.sale_line_id.price_unit',
                 'move_id.sale_line_id.product_uom',
                 'move_id.sale_line_id.tax_id',
                 'move_id.sale_line_id.gift',
                 'move_id.sale_line_id.order_id',
                 'qty_done',
                 'product_uom_id')
    def _compute_nd_price_unit(self):
        for line in self:
            price_unit = price_total = 0
            tax_ids = False
            gift = False
            nd_department_id = False
            nd_sale_type = False
            nd_transport_type = False
            nd_hospital_id = False
            nd_note = False
            nd_hospital_department = False

            if line.move_id:
                nd_note = line.move_id.picking_id.note

            if line.move_id and line.move_id.purchase_line_id:
                purchase_line_id = line.move_id.purchase_line_id
                price_unit = purchase_line_id.price_unit
                qty_done = line.product_uom_id._compute_quantity(line.qty_done, purchase_line_id.product_uom)
                price_total = price_unit * qty_done
                tax_ids = purchase_line_id.taxes_id
                gift = purchase_line_id.gift
                employee_ids = purchase_line_id.order_id.user_id.employee_ids
                if employee_ids:
                    nd_department_id = employee_ids[0].department_id.id

            elif line.move_id and line.move_id.sale_line_id:
                sale_line_id = line.move_id.sale_line_id
                price_unit = sale_line_id.price_unit
                qty_done = line.product_uom_id._compute_quantity(line.qty_done, sale_line_id.product_uom)
                price_total = price_unit * qty_done
                tax_ids = sale_line_id.tax_id
                gift = sale_line_id.gift
                sale_order = sale_line_id.order_id
                nd_department_id = sale_order.nd_department_id.id
                nd_sale_type = sale_order.nd_sale_type
                nd_transport_type = sale_order.nd_transport_type
                nd_hospital_id = sale_order.nd_hospital_id.id
                nd_hospital_department = sale_order.nd_hospital_department

            line.update({'nd_price_unit': price_unit,
                         'nd_price_total': price_total,
                         'tax_ids': tax_ids and tax_ids.ids,
                         'gift': gift,
                         'nd_department_id': nd_department_id,
                         'nd_sale_type': nd_sale_type,
                         'nd_transport_type': nd_transport_type,
                         'nd_hospital_id': nd_hospital_id,
                         'nd_hospital_department': nd_hospital_department,
                         'nd_note': nd_note,
                         })

    @api.depends('move_id.picking_code', 'move_id')
    def _compute_nd_type(self):
        for line in self:
            move_id = line.move_id
            ttype = False
            if move_id:
                picking_id = move_id.picking_id
                if move_id._is_return():
                    if move_id.picking_code == 'incoming':
                        ttype = 'sale_return'
                    elif move_id.picking_code == 'outgoing':
                        ttype = 'purchase_return'
                else:
                    if move_id.picking_code == 'incoming':
                        if not picking_id.wms_no and not picking_id.partner_id:
                            ttype = 'other_purchase'
                        else:
                            ttype = 'purchase'
                    elif move_id.picking_code == 'outgoing':
                        if not picking_id.wms_no and not picking_id.partner_id:
                            ttype = 'other_sale'
                        else:
                            ttype = 'sale'
                    elif move_id.picking_code == 'internal':
                        ttype = 'internal'
            line.nd_type = ttype


class StockPickingAddField(models.Model):

    _inherit = 'stock.picking'

    stock_move_line_total = fields.Float('含税总价', compute="_compute_move_line_total", store=True)

    @api.depends('move_line_ids')
    def _compute_move_line_total(self):
        for res in self:
            res.stock_move_line_total = sum(res.move_line_ids.mapped('nd_price_total'))
